package com.hourse.platform.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SawggerConfig {

  @Value("${swagger.enable}")
  private boolean swaggerEnable;


  @Bean
  public Docket customDocket() {

    ParameterBuilder token = new ParameterBuilder();
    ParameterBuilder stype = new ParameterBuilder();
    List<Parameter> pars = new ArrayList<>();
    token.name("token").description("token")
      .modelRef(new ModelRef("string")).parameterType("header")
      .required(false).build();

    stype.name("stype").description("系统标识")
      .modelRef(new ModelRef("string")).parameterType("header")
      .defaultValue("user")
      .required(false).build();

    pars.add(token.build());
    pars.add(stype.build());
    return new Docket(DocumentationType.SWAGGER_2)
      .enable(swaggerEnable)
      .apiInfo(apiInfo())
      .select()
      .apis(RequestHandlerSelectors.basePackage("com.hourse"))
      .paths(PathSelectors.any())
      .build().globalOperationParameters(pars);
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
      .title(" 交易平台 API")
      .version("1.0.0")
      .build();
  }
}
